Undo silly design decision by me; namely, to do any checking of values in the Parse classes.
Instead, it should be done in the Command classes, so that the web interface need not duplicate code.
import org.xenoserver.control.CommandPartitionAdd;
import org.xenoserver.control.Defaults;
import org.xenoserver.control.Library;
-import org.xenoserver.control.Partition;
-import org.xenoserver.control.PartitionManager;
-import org.xenoserver.control.Settings;
public class ParsePartitionsAdd extends CommandParser {
- public void parse(Defaults d, LinkedList args) throws ParseFailedException, CommandFailedException {
- boolean force = getFlagParameter(args, 'f');
- String partition_name = getStringParameter(args, 'p', "");
- String size = getStringParameter(args, 'c', "128M");
-
- if (partition_name.equals("")) {
- throw new ParseFailedException("Expected -p<partition_name>");
- }
-
- long chunksize = Library.parseSize( size ) / Settings.SECTOR_SIZE;
- if ( chunksize <= 0 ) {
- throw new CommandFailedException("Chunk size " + size + " is smaller than sector size.");
- }
-
- // Initialise the partition manager and look up the partition
- loadState();
- Partition p = PartitionManager.IT.getPartition(partition_name);
-
- if ( p == null ) {
- throw new CommandFailedException("Partition " + partition_name + " does not exist.");
- }
-
- // Check if this partition belongs to the VDM
- if (p.isXeno() && !force) {
- throw new CommandFailedException("Refusing to add partition as it is already allocated to the virtual disk manager. Use -f if you are sure.");
- }
-
- String output = new CommandPartitionAdd( p, chunksize ).execute();
- if ( output != null ) {
- System.out.println( output );
+ public void parse(Defaults d, LinkedList args)
+ throws ParseFailedException, CommandFailedException {
+ boolean force = getFlagParameter(args, 'f');
+ String partition_name = getStringParameter(args, 'p', "");
+ String size = getStringParameter(args, 'c', "128M");
+
+ if (partition_name.equals("")) {
+ throw new ParseFailedException("Expected -p<partition_name>");
+ }
+
+ loadState();
+ String output =
+ new CommandPartitionAdd(partition_name, Library.parseSize(size),force)
+ .execute();
+ if (output != null) {
+ System.out.println(output);
+ }
+ saveState();
}
- saveState();
- }
- public String getName() {
- return "add";
- }
+ public String getName() {
+ return "add";
+ }
- public String getUsage() {
- return "-p<partition_name> [-f] [-c<chunk_size>]";
- }
+ public String getUsage() {
+ return "-p<partition_name> [-f] [-c<chunk_size>]";
+ }
- public String getHelpText() {
- return "Add the specified partition to the virtual disk manager's free\n" +
- "space. -c changes the default chunk size. -f forces add.";
- }
+ public String getHelpText() {
+ return "Add the specified partition to the virtual disk manager's free\n"
+ + "space. -c changes the default chunk size. -f forces add.";
+ }
}
import org.xenoserver.control.CommandPhysicalGrant;
import org.xenoserver.control.Defaults;
import org.xenoserver.control.Mode;
-import org.xenoserver.control.Partition;
-import org.xenoserver.control.PartitionManager;
public class ParsePhysicalGrant extends CommandParser {
- public void parse(Defaults d, LinkedList args) throws ParseFailedException, CommandFailedException {
- int domain_id = getIntParameter(args, 'n', 0);
- boolean force = getFlagParameter(args, 'f');
- String partition_name = getStringParameter(args, 'p', "");
- boolean write = getFlagParameter(args, 'w');
-
- if (domain_id == 0) {
- throw new ParseFailedException("Expected -n<domain_id>");
- }
- if (partition_name.equals("")) {
- throw new ParseFailedException("Expected -p<partition_name>");
- }
-
- Mode mode;
- if (write) {
- mode = Mode.READ_WRITE;
- } else {
- mode = Mode.READ_ONLY;
- }
-
- // Initialise the partition manager and look up the partition
- loadState();
- Partition p = PartitionManager.IT.getPartition(partition_name);
-
- if ( p == null ) {
- throw new CommandFailedException("Partition " + partition_name + " does not exist.");
- }
-
- // Check if this partition belongs to the VDM
- if (p.isXeno() && !force) {
- throw new CommandFailedException("Refusing to grant physical access as the given partition is allocated to the virtual disk manager. Use -f if you are sure.");
- }
-
- String output = new CommandPhysicalGrant( d, domain_id, p, mode ).execute();
- if ( output != null ) {
- System.out.println( output );
+ public void parse(Defaults d, LinkedList args)
+ throws ParseFailedException, CommandFailedException {
+ int domain_id = getIntParameter(args, 'n', 0);
+ boolean force = getFlagParameter(args, 'f');
+ String partition_name = getStringParameter(args, 'p', "");
+ boolean write = getFlagParameter(args, 'w');
+
+ if (domain_id == 0) {
+ throw new ParseFailedException("Expected -n<domain_id>");
+ }
+ if (partition_name.equals("")) {
+ throw new ParseFailedException("Expected -p<partition_name>");
+ }
+
+ Mode mode;
+ if (write) {
+ mode = Mode.READ_WRITE;
+ } else {
+ mode = Mode.READ_ONLY;
+ }
+
+ // Initialise the partition manager and look up the partition
+ loadState();
+ String output =
+ new CommandPhysicalGrant(d, domain_id, partition_name, mode, force)
+ .execute();
+ if (output != null) {
+ System.out.println(output);
+ }
}
- }
- public String getName() {
- return "grant";
- }
+ public String getName() {
+ return "grant";
+ }
- public String getUsage() {
- return "-n<domain_id> -p<partition_name> [-f] [-w]";
- }
+ public String getUsage() {
+ return "-n<domain_id> -p<partition_name> [-f] [-w]";
+ }
- public String getHelpText() {
- return "Grant the specified domain access to the given partition. -w grants" +
- " read-write instead of read-only. -f forcibly grants access.";
- }
+ public String getHelpText() {
+ return "Grant the specified domain access to the given partition. -w grants"
+ + " read-write instead of read-only. -f forcibly grants access.";
+ }
}
import org.xenoserver.control.CommandFailedException;
import org.xenoserver.control.CommandPhysicalRevoke;
import org.xenoserver.control.Defaults;
-import org.xenoserver.control.Partition;
-import org.xenoserver.control.PartitionManager;
public class ParsePhysicalRevoke extends CommandParser {
public void parse(Defaults d, LinkedList args)
// Initialise the partition manager and look up the partition
loadState();
- Partition p = PartitionManager.IT.getPartition(partition_name);
-
- if (p == null) {
- throw new CommandFailedException(
- "Partition " + partition_name + " does not exist.");
- }
-
- String output = new CommandPhysicalRevoke(d, domain_id, p).execute();
+ String output = new CommandPhysicalRevoke(d, domain_id, partition_name).execute();
if (output != null) {
System.out.println(output);
}
import org.xenoserver.control.CommandVbdCreatePhysical;
import org.xenoserver.control.Defaults;
import org.xenoserver.control.Mode;
-import org.xenoserver.control.Partition;
-import org.xenoserver.control.PartitionManager;
-import org.xenoserver.control.VirtualDisk;
-import org.xenoserver.control.VirtualDiskManager;
public class ParseVbdCreate extends CommandParser {
public void parse(Defaults d, LinkedList args)
loadState();
String output;
if (vd_key.equals("")) {
- Partition p = PartitionManager.IT.getPartition(partition_name);
- if ( p == null ) {
- throw new CommandFailedException("No partition " + partition_name + " exists" );
- }
-
- output = new CommandVbdCreatePhysical( p, domain_id, vbd_num, mode ).execute();
+ output = new CommandVbdCreatePhysical( partition_name, domain_id, vbd_num, mode ).execute();
} else {
- VirtualDisk vd = VirtualDiskManager.IT.getVirtualDisk(vd_key);
- if (vd == null) {
- throw new CommandFailedException(
- "No virtual disk with key " + vd_key);
- }
-
output =
- new CommandVbdCreate(vd, domain_id, vbd_num, mode).execute();
+ new CommandVbdCreate(vd_key, domain_id, vbd_num, mode).execute();
}
if (output != null) {
System.out.println(output);
import org.xenoserver.control.CommandVdCreate;
import org.xenoserver.control.Defaults;
import org.xenoserver.control.Library;
-import org.xenoserver.control.Settings;
public class ParseVdCreate extends CommandParser {
public void parse(Defaults d, LinkedList args)
long size = Library.parseSize(size_s);
loadState();
- String output =
- new CommandVdCreate(name, size / Settings.SECTOR_SIZE, expiry)
- .execute();
+ String output = new CommandVdCreate(name, size, expiry).execute();
if (output != null) {
System.out.println(output);
}
import org.xenoserver.control.CommandFailedException;
import org.xenoserver.control.CommandVdDelete;
import org.xenoserver.control.Defaults;
-import org.xenoserver.control.VirtualDiskManager;
public class ParseVdDelete extends CommandParser {
public void parse(Defaults d, LinkedList args)
}
loadState();
- if (VirtualDiskManager.IT.getVirtualDisk(vd_key) == null) {
- throw new CommandFailedException(
- "Virtual disk " + vd_key + " does not exist");
- }
-
String output = new CommandVdDelete(vd_key).execute();
if (output != null) {
System.out.println(output);
* Add a disk partition to the VirtualDiskManager as a XenoPartition.
*/
public class CommandPartitionAdd extends Command {
+ /** True to force creation. */
+ private boolean force;
/** Partition to add as a XenoPartition. */
- private Partition partition;
- /** Chunk size to split partition into (in sectors). */
+ private String partition_name;
+ /** Chunk size to split partition into (in bytes). */
private long chunksize;
/**
* Constructor for CommandPartitionAdd.
- * @param partition Partition to add.
- * @param chunksize Chunk size to split partition into (in sectors).
+ * @param partition_name Partition to add.
+ * @param chunksize Chunk size to split partition into (in bytes).
+ * @param force True to force creation.
*/
- public CommandPartitionAdd(Partition partition, long chunksize) {
- this.partition = partition;
+ public CommandPartitionAdd(String partition_name, long chunksize, boolean force) {
+ this.partition_name = partition_name;
this.chunksize = chunksize;
+ this.force = force;
}
/**
* @see org.xenoserver.control.Command#execute()
*/
public String execute() throws CommandFailedException {
- VirtualDiskManager.IT.addPartition(partition, chunksize);
- PartitionManager.IT.addXenoPartition(partition);
- return "Added partition " + partition.getName();
+ Partition p = PartitionManager.IT.getPartition(partition_name);
+ if (p == null) {
+ throw new CommandFailedException(
+ "Partition " + partition_name + " does not exist.");
+ }
+ // Check if this partition belongs to the VDM
+ if (p.isXeno() && !force) {
+ throw new CommandFailedException("Refusing to add partition as it is already allocated to the virtual disk manager. Use -f if you are sure.");
+ }
+
+ long size = chunksize / Settings.SECTOR_SIZE;
+ if ( chunksize <= 0 ) {
+ throw new CommandFailedException("Chunk size is smaller than sector size.");
+ }
+
+ VirtualDiskManager.IT.addPartition(p, size);
+ PartitionManager.IT.addXenoPartition(p);
+ return "Added partition " + p.getName();
}
}
/** Domain ID to grant access for */
private int domain_id;
/** Partition to grant access to */
- private Partition partition;
+ private String partition_name;
/** Access mode to grant */
private Mode mode;
+ /** True to force grant */
+ private boolean force;
/**
* Constructor for CommandPhysicalGrant.
* @param domain_id Domain to grant access for.
* @param partition Partition to grant access to.
* @param mode Access mode to grant.
+ * @param force True to force grant
*/
public CommandPhysicalGrant(
Defaults d,
int domain_id,
- Partition partition,
- Mode mode) {
+ String partition,
+ Mode mode,
+ boolean force) {
this.d = d;
this.domain_id = domain_id;
- this.partition = partition;
+ this.partition_name = partition;
this.mode = mode;
+ this.force = force;
}
/**
Runtime r = Runtime.getRuntime();
String output = null;
+ Partition partition = PartitionManager.IT.getPartition(partition_name);
+
+ if ( partition == null ) {
+ throw new CommandFailedException("Partition " + partition_name + " does not exist.");
+ }
+
+ // Check if this partition belongs to the VDM
+ if (partition.isXeno() && !force) {
+ throw new CommandFailedException("Refusing to grant physical access as the given partition is allocated to the virtual disk manager. Use -f if you are sure.");
+ }
+
try {
Process start_p;
String start_cmdarray[] = new String[7];
/** Domain to revoke access from */
private int domain_id;
/** Partition to revoke access to */
- private Partition partition;
+ private String partition_name;
/**
* Constructor for CommandPhysicalRevoke.
* @param domain_id Domain to revoke access from.
* @param partition Partition to revoke access to.
*/
- public CommandPhysicalRevoke(Defaults d, int domain_id, Partition partition) {
+ public CommandPhysicalRevoke(Defaults d, int domain_id, String partition) {
this.d = d;
this.domain_id = domain_id;
- this.partition = partition;
+ this.partition_name = partition;
}
/**
Runtime r = Runtime.getRuntime();
String output = null;
+ Partition partition = PartitionManager.IT.getPartition(partition_name);
+
+ if (partition == null) {
+ throw new CommandFailedException(
+ "Partition " + partition_name + " does not exist.");
+ }
+
try {
Process start_p;
String start_cmdarray[] = new String[5];
*/
public class CommandVbdCreate extends Command {
/** Virtual disk to map to. */
- private VirtualDisk vd;
+ private String vd_key;
/** Domain to create VBD for. */
private int domain_id;
/** VBD number to use. */
* @param mode Access mode to grant.
*/
public CommandVbdCreate(
- VirtualDisk vd,
+ String vd,
int domain_id,
int vbd_num,
Mode mode) {
- this.vd = vd;
+ this.vd_key = vd;
this.domain_id = domain_id;
this.vbd_num = vbd_num;
this.mode = mode;
* @see org.xenoserver.control.Command#execute()
*/
public String execute() throws CommandFailedException {
- VirtualBlockDevice vbd;
+ VirtualDisk vd = VirtualDiskManager.IT.getVirtualDisk(vd_key);
+ if (vd == null) {
+ throw new CommandFailedException(
+ "No virtual disk with key " + vd_key);
+ }
- vbd =
+ VirtualBlockDevice vbd =
VirtualDiskManager.IT.createVirtualBlockDevice(
vd,
domain_id,
*/
public class CommandVbdCreatePhysical extends Command {
/** Virtual disk to map to. */
- private Partition partition;
+ private String partition_name;
/** Domain to create VBD for. */
private int domain_id;
/** VBD number to use. */
* @param mode Access mode to grant.
*/
public CommandVbdCreatePhysical(
- Partition partition,
+ String partition,
int domain_id,
int vbd_num,
Mode mode) {
- this.partition = partition;
+ this.partition_name = partition;
this.domain_id = domain_id;
this.vbd_num = vbd_num;
this.mode = mode;
* @see org.xenoserver.control.Command#execute()
*/
public String execute() throws CommandFailedException {
- VirtualDisk vd = new VirtualDisk("vbd:"+partition.getName());
- vd.addPartition(partition,partition.getNumSects());
+ Partition partition = PartitionManager.IT.getPartition(partition_name);
+ if (partition == null) {
+ throw new CommandFailedException(
+ "No partition " + partition_name + " exists");
+ }
+
+ VirtualDisk vd = new VirtualDisk("vbd:" + partition.getName());
+ vd.addPartition(partition, partition.getNumSects());
- VirtualBlockDevice vbd = new VirtualBlockDevice(
- vd,
- domain_id,
- vbd_num,
- mode);
+ VirtualBlockDevice vbd =
+ new VirtualBlockDevice(vd, domain_id, vbd_num, mode);
String command = vd.dumpForXen(vbd);
fw.flush();
fw.close();
} catch (IOException e) {
- throw new CommandFailedException("Could not write VBD details to /proc/xeno/dom0/vhd", e);
+ throw new CommandFailedException(
+ "Could not write VBD details to /proc/xeno/dom0/vhd",
+ e);
}
return "Created virtual block device "
public class CommandVdCreate extends Command {
/** Name of new disk. */
private String name;
- /** Size of new disk in sectors. */
+ /** Size of new disk in bytes. */
private long size;
/** Expiry date of new disk. */
private Date expiry;
/**
* Constructor for CommandVdCreate.
* @param name Name of new virtual disk.
- * @param size Size in sectors.
+ * @param size Size in bytes.
* @param expiry Expiry time, or null for never.
*/
public CommandVdCreate(String name, long size, Date expiry) {
*/
public String execute() throws CommandFailedException {
VirtualDisk vd =
- VirtualDiskManager.IT.createVirtualDisk(name, size, expiry);
+ VirtualDiskManager.IT.createVirtualDisk(
+ name,
+ size / Settings.SECTOR_SIZE,
+ expiry);
if (vd == null) {
throw new CommandFailedException("Not enough free space to create disk");
}
* @see org.xenoserver.control.Command#execute()
*/
public String execute() throws CommandFailedException {
+ if (VirtualDiskManager.IT.getVirtualDisk(key) == null) {
+ throw new CommandFailedException(
+ "Virtual disk " + key + " does not exist");
+ }
+
VirtualDiskManager.IT.deleteVirtualDisk(key);
return "Deleted virtual disk " + key;
}
* @param other Other partition to compare to.
* @return True if they are identical.
*/
- public boolean identical(Partition other) {
+ boolean identical(Partition other) {
return this.major == other.major
&& this.minor == other.minor
&& this.blocks == other.blocks
/**
* @return An Extent covering this partiton.
*/
- public Extent toExtent() {
+ Extent toExtent() {
return new Extent(getDisk(),start_sect,nr_sects);
}
* @param e Extent to compare this partition to.
* @return True if this partition covers the same disk area as the given extent.
*/
- public boolean matchesExtent(Extent e) {
+ boolean matchesExtent(Extent e) {
return e.getDisk() == getDisk()
&& e.getOffset() == start_sect
&& e.getSize() == nr_sects;
* Reset the expiry time for this virtual disk.
* @param expiry The new expiry time, or null for never.
*/
- public void refreshExpiry(Date expiry) {
+ void refreshExpiry(Date expiry) {
this.expiry = expiry;
}
}
* @param partition The partition to add.
* @param chunkSize The chunk size to split the partition into, in sectors.
*/
- public void addPartition(Partition partition, long chunkSize) {
+ void addPartition(Partition partition, long chunkSize) {
freeDisk.addPartition(partition, chunkSize);
}
* @param expiry The expiry time, or null for never.
* @return null if not enough space is available
*/
- public VirtualDisk createVirtualDisk(String name, long size, Date expiry) {
+ VirtualDisk createVirtualDisk(String name, long size, Date expiry) {
if (freeDisk.getSize() < size) {
return null;
}
* Delete a virtual disk, and put its extents back into the free pool.
* @param key The key of the disk to delete.
*/
- public void deleteVirtualDisk(String key) {
+ void deleteVirtualDisk(String key) {
VirtualDisk vd;
vd = (VirtualDisk) virtualDisks.get(key);
* @param mode The mode to create the device with.
* @return The newly created virtual block device.
*/
- public VirtualBlockDevice createVirtualBlockDevice(
+ VirtualBlockDevice createVirtualBlockDevice(
VirtualDisk vd,
int domain,
int vbdNum,
* @param domain Domain owning the device.
* @param vbdNum The vbd number within the domain.
*/
- public void deleteVirtualBlockDevice(int domain, int vbdNum) {
+ void deleteVirtualBlockDevice(int domain, int vbdNum) {
Object hash = hashVBD(domain, vbdNum);
virtualBlockDevices.remove(hash);
}
/**
* Flush all virtual block devices.
*/
- public void flushVirtualBlockDevices() {
+ void flushVirtualBlockDevices() {
/* isn't automatic garbage collection wonderful? */
virtualBlockDevices = new LinkedHashMap(100);
}